package com.zl.view;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.zl.dao.BookTypeDao;
import com.zl.model.BookType;
import com.zl.util.ZLString;
import com.zl.util.ZLUtil;

import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.awt.event.ActionEvent;

public class AddBookTypeFrame extends JInternalFrame {
	private JTextField textField;
	private JTextArea textArea;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddBookTypeFrame frame = new AddBookTypeFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddBookTypeFrame() {
		setTitle("添加图书类别");
		setClosable(true);
		setIconifiable(true);
		setBounds(100, 100, 495, 366);
		
		JLabel lblNewLabel = new JLabel("图书类别名称：");
		
		JLabel lblNewLabel_1 = new JLabel("图书类别描述：");
		
		textField = new JTextField();
		textField.setColumns(10);
		
		textArea = new JTextArea();
		textArea.setLineWrap(true);
		
		JButton button = new JButton("添加");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				submitBookType(e);
			}
		});
		button.setIcon(new ImageIcon(AddBookTypeFrame.class.getResource("/images/add.png")));
		
		JButton button_1 = new JButton("重置");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetInputInfo(e);
			}
		});
		button_1.setIcon(new ImageIcon(AddBookTypeFrame.class.getResource("/images/reset.png")));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(82)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblNewLabel_1)
							.addGap(18)
							.addComponent(textArea))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblNewLabel)
							.addGap(18)
							.addComponent(textField, GroupLayout.PREFERRED_SIZE, 216, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(64, Short.MAX_VALUE))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(129)
					.addComponent(button)
					.addPreferredGap(ComponentPlacement.RELATED, 114, Short.MAX_VALUE)
					.addComponent(button_1)
					.addGap(112))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(56)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_1)
						.addComponent(textArea, GroupLayout.PREFERRED_SIZE, 123, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(button)
						.addComponent(button_1))
					.addContainerGap(50, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);

	}

	/**
	 * 图书类别提交
	 * @param e
	 */
	private void submitBookType(ActionEvent e) {
		// TODO Auto-generated method stub
		String bookTypeName = this.textField.getText();
		String bookTypeDesc = this.textArea.getText();
		
		if (ZLString.isEmpty(bookTypeName)) {
			JOptionPane.showMessageDialog(null, "图书类别名称不能为空");
			return;
		}
		
		boolean isAdmin = LoginFrame.loginSuccessUser.getIsAdmin();
		if (!isAdmin) {
			JOptionPane.showMessageDialog(null, "该用户暂无添加图书类别的权限");
			return;
		}
		
		BookType bookType = new BookType(bookTypeName, bookTypeDesc);
		Connection connection = null;
		
		try {
			
			connection = ZLUtil.getConnect();
			int success = BookTypeDao.add(connection, bookType);
			
			if (success == 1) {
				JOptionPane.showMessageDialog(null, "图书类别添加成功！");
				
				//重置输入框
				this.textArea.setText("");
				this.textField.setText("");
			}else {
				JOptionPane.showMessageDialog(null, "图书类别添加失败！");
			}
			
		} catch (Exception e2) {
			// TODO: handle exception
			e2.printStackTrace();
			JOptionPane.showMessageDialog(null, "图书类别添加失败！");
			
		} finally {
			
			try {
				connection.close();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	/**
	 * 重置图书类别信息
	 * @param e
	 */
	private void resetInputInfo(ActionEvent e) {
		// TODO Auto-generated method stub
		this.textField.setText("");
		this.textArea.setText("");
	}
}
